<template>
  <div class="addGroup">
    <el-dialog
      :visible="visible"
      :close-on-click-modal="false"
      width="500px"
      :modal-append-to-body="false"
      :show-close="false"
    >
      <div slot="title" class="header">
        <div class="left">
          <div class="icon" />
          <div class="title">新增用户</div>
        </div>
        <div class="close_icon" @click="close" />
      </div>
      <div v-loading="loading">
        <el-form ref="userForm" label-width="70px" :model="userForm" :rules="rules" hide-required-asterisk>
          <el-form-item prop="userName" label="用户名称">
            <el-input
              v-model="userForm.userName"
              type="text"
              maxlength="45"
              show-word-limit
              placeholder="只能输入数字，字母和下划线“_”"
            />
          </el-form-item>
          <el-form-item prop="description" label="用户描述">
            <el-input
              v-model="userForm.description"
              type="textarea"
              placeholder="请输入内容"
              maxlength="45"
              show-word-limit
            />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="footer">
        <el-button class="btn" plain @click="close">取消</el-button>
        <el-button class="btn" type="primary" :loading="loading" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { key_user_add } from '@/api/privateKey'
export default {
  name: 'AccountEdit',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    // 这里存放数据
    return {
      loading: false,
      userForm: {
        userName: '',
        description: ''
      },
      rules: {
        userName: { required: true, message: '请输入用户名称', trigger: 'blur' }
      }
    }
  },
  // 监听属性 类似于data概念
  computed: {
    ...mapState({
      myInfo: (state) => state.basicSettings.myInfo
    })
  },
  // 监控data中的数据变化
  watch: {
    'userForm.userName'(n, o) {
      this.userForm.userName = this.userForm.userName.replace(/[^\w_]/g, '')
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  async created() {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() { },
  // 生命周期 - keep-alive
  async activated() {},
  // 方法集合
  methods: {
    submit() {
      this.$refs.userForm.validate(async(valid) => {
        if (valid) {
          this.loading = true
          try {
            const param = {
              account: this.myInfo.account,
              userName: this.userForm.userName,
              description: this.userForm.description,
              groupId: 1
            }
            const { code, message } = await key_user_add(param)
            if (!code) {
              this.close()
              this.loading = false
              this.$parent.loadData()
              this.$messageOnce.success('添加成功！')
            } else {
              this.$messageOnce.warning(message)
            }
          } catch (error) {
            console.error(error)
            this.loading = false
          }
        } else {
          return false
        }
      })
    },
    close() {
      this.$refs.userForm.resetFields()
      this.$emit('close')
    }
  },
  // 进入页面前
  beforeRouteEnter(to, from, next) {
    next((vm) => {})
  },
  // 离开页面前
  beforeRouteLeave(to, from, next) {
    next()
  }
}
</script>
<style lang="scss" scoped>
.addGroup {
	.header{
		width: 100%;
		display: flex;
		justify-content: space-between;
    align-items: center;
    background: #FAFAFA;
		.left{
			display: flex;
			.icon{
				background: url('../../assets/dialog/add_icon.png');
				@include dialog_head_icons;
			}
			.title{
				font-size: 14px;
				font-family: SourceHanSansCN-Medium, SourceHanSansCN;
				font-weight: 500;
				color: #000000;
				line-height: 30px;
			}
		}
    .close_icon {
				width:25px;
        height: 24px;
				background: url('../../assets/close-icon.png');
				background-size: 100%;
        cursor: pointer;
		}
	}
  .footer {
    text-align: center;
    .btn{
      width: 80px;
    }
  }
}

::v-deep .el-dialog__body {
    padding: 25px 30px 30px 30px;
    color: #606266;
    font-size: 14px;
    word-break: break-all;
}

::v-deep .el-dialog__footer{
  height: 80px;
  padding: 20px;
  box-shadow: 0px -5px 20px 0px rgba(71, 73, 155, 0.06);
  border-radius: 0px 0px 4px 4px;
}

::v-deep .el-input__inner{
	border-radius: 4px !important;
  padding-right: 45px !important;
}
</style>
